package com.example.homemodule.adapter;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.examination.mlib.baseold.AnswerDetailsListEntity;
import com.example.homemodule.listener.OnAnswerCardListener;
import com.example.homemodule.view.ui.AnswerCardFragment;
import com.yilijk.base.utils.ListUtil;

import java.util.List;

public class AnswerPagerAdapter extends FragmentStatePagerAdapter {


    private List<AnswerDetailsListEntity.DataItem> beans;
    private OnAnswerCardListener answerCardListener;

    public AnswerPagerAdapter(@NonNull FragmentManager fm, int behavior, List<AnswerDetailsListEntity.DataItem> beans, OnAnswerCardListener answerCardListener) {
        super(fm, behavior);
        this.beans = beans;
        this.answerCardListener = answerCardListener;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        AnswerCardFragment fragment = AnswerCardFragment.getInstance(position,beans.get(position), ListUtil.getSize(beans));
        fragment.setListener(answerCardListener);
        return fragment;
    }

    @Override
    public int getCount() {
        return ListUtil.getSize(beans);
    }

    public static class LoopTransformer implements ViewPager.PageTransformer {

        private static final float MIN_SCALE = 0.9f;

        @Override
        public void transformPage(@NonNull View view, float position) {
            /**
             * 过滤那些 <-1 或 >1 的值，使它区于【-1，1】之间
             */
            if (position < -1) {
                position = -1;
            } else if (position > 1) {
                position = 1;
            }
            /**
             * 判断是前一页 1 + position ，右滑 pos -> -1 变 0
             * 判断是后一页 1 - position ，左滑 pos -> 1 变 0
             */
            float tempScale = position < 0 ? 1 + position : 1 - position; // [0,1]
            float scaleValue = MIN_SCALE + tempScale * 0.1f; // [0,1]

//            view.setPivotY(view.getHeight() / 2);
//            view.setPivotX(view.getWidth() / 2);

            view.setScaleX(scaleValue);
            view.setScaleY(scaleValue);

        }
    }
}
